<template>
  <div>
    <template v-if="shipment.planType === PLAN_TYPE_FL()">
      <shipment-box-table1 :shipment="shipment" :table-data="tableData" :hidden-table-data="hiddenTableData"></shipment-box-table1>
    </template>
    <template v-else-if="shipment.planType === PLAN_TYPE_MW()">
      <shipment-box-table2 :shipment="shipment" :table-data="tableData" :hidden-table-data="hiddenTableData"></shipment-box-table2>
    </template>

  </div>
</template>

<script>
import {
  getBoxStatusStr,
  getBoxTypeStr,
  STATUS_CREATED,
  STATUS_LISTED,
  STATUS_RECEIVED,
  STATUS_SORTED
} from "@/utils/boxLabel";
import {WAIT_FL_RECEIVE} from "@/utils/shipment";
import ReElTable from "@/components/Common/ReElTable.vue";
import {saveBoxLabelGoodsNum} from "@/api/shipmentV2";
import MLCodeListColumn from "@/views/stock/components/MLCodeListColumn.vue";
import MLCodeColumn from "@/views/stock/components/MLCodeColumn.vue";
import {PLAN_TYPE_FL, PLAN_TYPE_MW} from "@/utils/shippingPlan";
import ShipmentBoxTable1 from "@/views/shipment/components/ShipmentBoxTable1.vue";
import ShipmentBoxTable2 from "@/views/shipment/components/ShipmentBoxTable2.vue";

export default {
  name: 'ShipmentBoxTable',
  components: {ShipmentBoxTable2, ShipmentBoxTable1},
  props: {
    tableData: {
      type : Array,
      required: true,
    },
    shipment: {
     type: Object,
     required: true,
    },
    hiddenTableData: {
      type: Boolean,
      default: true,
    }
  },
  computed: {
    canEdit(){
      return this.shipment.status2 === WAIT_FL_RECEIVE;
    }
  },
  methods: {
    PLAN_TYPE_MW() {
      return PLAN_TYPE_MW
    },
    PLAN_TYPE_FL() {
      return PLAN_TYPE_FL
    },
    STATUS_LISTED() {
      return STATUS_LISTED
    },
    STATUS_SORTED() {
      return STATUS_SORTED
    },
    getBoxStatusStr,
    STATUS_CREATED() {
      return STATUS_CREATED
    },
    STATUS_RECEIVED() {
      return STATUS_RECEIVED
    },
    getBoxTypeStr,
    changeSize(row){
      const weight = row.weight
      const volumeWeight = row.volumeWeight
      const squareNumber = row.squareNumber
      this.tableData.filter(item => item.boxId === row.boxId)
        .forEach(item => {
          item.weight = weight
          item.volumeWeight = volumeWeight
          item.squareNumber = squareNumber
          // 都填了
          if(item.weight && item.volumeWeight && item.squareNumber){
            item.status = STATUS_RECEIVED
          }else{
            item.status = STATUS_CREATED
          }
        })

    },
    getBoxRlNum(row){
      return this.tableData.filter(item => item.boxId === row.boxId).reduce((prev, cur) => prev + (cur.rlGoodsNum ? cur.rlGoodsNum : 0), 0)
    },
    saveRlGoodsNum(row){
      const data = this.tableData.filter(item => item.boxId === row.boxId)
        .map(item => {
          return {
            id: item.id,
            boxId: item.boxId,
            rlGoodsNum: item.rlGoodsNum
          }
        })
      const postData = {
        boxData: data,
      }
      saveBoxLabelGoodsNum(postData)
        .then(res=>{
          this.$successMessage(res.message)
        })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      const mergeCell = this.mergeCell()
      if(mergeCell[column.property]){
        if(mergeCell[column.property][rowIndex]) {
          return [mergeCell[column.property][rowIndex], 1]
        } else {
          // 如果为0则为需要合并的行
          return [0, 0];
        }
      }
    },
    mergeCell(){
      const mergeObj = {};
      const mergeField = 'boxId'; // 合并的字段
      const mergeFieldArr = [
        'boxId',
        'boxType',
        'weight',
        'squareNumber',
        'volumeWeight',
        'status',
        'boxExNum',
        'boxRlNum',
        'op',
      ]; // 合并的单元格

      mergeFieldArr.forEach(key=>{
        mergeObj[key] = [1];
      })

      let mergeFieldKey = 0;
      this.tableData.forEach((item,index)=>{
        // 第一列数据不处理
        if(index === 0){
          return;
        }
        // 根据合并字段 合并
        if(item[mergeField] && item[mergeField] === this.tableData[index - 1][mergeField]){
          mergeFieldArr.forEach(key=>{
            mergeObj[key][mergeFieldKey] += 1;
            mergeObj[key].push(0); // 添加一个0 站位
          })
        }else{
          mergeFieldKey = index; // 记录当前行
          mergeFieldArr.forEach(key=>{
            mergeObj[key].push(1); // 添加一个1
          })
        }
      })

      return mergeObj;
    },

  },

}
</script>

<style scoped lang="scss">

</style>
